import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseName:'',  //维修的房屋
    houseList: [
      { name: '无审核通过房屋' },
    ],
    repairName:'',   //维修项目
    repairItemId: '',
    repairItem: [{ name: '水路卫浴' }, { name: '电路灯具' }, { name: '管道疏通' }, { name: '开锁换锁' }],
    attachment: [
      { url: '/repair_pkg/static/uploads/attachment.jpg' },
      { url: '/repair_pkg/static/uploads/attachment.jpg' },
    ],
    houseInfo:'',
    id:undefined,
    houseId:'',    //维修房屋的id
    appointment: '',  //预约时间
    description:'' ,   //问题描述
    mobile:'',  //手机号
    attachment: [],  //问题附件
    status: 0
  },
  rules: {
    houseName:[
      {required: true, message: '请选择房屋'},
    ],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1\d{10}$/, message: '请检查手机号码是否正确!' },
    ],
    repairName:[
      { required: true, message: '请选择维修项目!' },
    ],
    appointment:[
      { required: true, message: '请选择预约日期!' },
    ],
    description:[
      { required: true, message: '请进行问题描述!' }
    ],
    attachment:[
      { required: true, message: '请选上传问题附件!' }
    ]
  },
  // 报修房屋显示和隐藏
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  // 点击底部取消按钮
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true, })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async onSubmit() { 
    // 校验  rules 中定义的全部数据
    const isAllValid = this.validate()
    console.log('isAllValid',isAllValid);
    if (!isAllValid) return 

    const {id,houseId,repairItemId,mobile,appointment,description,attachment} = this.data
    const body = {id,houseId,repairItemId,mobile,appointment,description,attachment}
    const res = await wx.http.post('/repair',body)
    console.log('提交报修成功',res);

    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  selectHouse(e){
    console.log('选择维修的房屋',e.detail);
    this.setData({
      houseName:e.detail.name,
      houseId:e.detail.id
    })
  },
  selectRepair(e){
    console.log('选择维修的项目',e.detail);
    this.setData({
      repairName:e.detail.name,
      repairItemId:e.detail.id
    })
  },
  async getDetail(id){
    const res = await wx.http.get(`/repair/${id}`)
    console.log('报修详情',res);
    this.setData({
      appointment:res.data.appointment,
      description:res.data.description,
      houseName:res.data.houseInfo,
      mobile:res.data.mobile,
      repairName:res.data.repairItemName,
      attachment:res.data.attachment,
    })
  },
  onLoad({id}){
    console.log(id);
    if (id) {
      this.getDetail(id)
    }
    // 查询审核通过的房屋
    this.getPassHouseList()
    // 查询维修项目
    this.getrepairItem()
  },
  async getPassHouseList(){
    const res = await wx.http.get('/house')
    console.log('审核通过的房屋',res);
    // 假设后端返回的数组中小区名字不是name，则使用数组的映射
    this.setData({
      houseList:res.data,
    })
  },
  async getrepairItem(){
    const res = await wx.http.get('/repairItem')
    console.log('维修项目-选项',res);
    this.setData({
      repairItem:res.data,
    })
  },
  // 选择日期
  confirmDate(ev){
    this.closeDateLayer()
    // console.log('ev',ev.detail);
    // const date = new Date(ev.detail)
    // console.log('date',date);
    // const year = date.getFullYear()
    // const month = date.getMonth() + 1
    // const day = date.getDate()
    // 方式二
    // date.toLocaleDateString('zh-CN').replaceAll('/','-')
    this.setData({
      // appointment:`${year}-${month}-${day}`,
      // appointment:date.toLocaleDateString('zh-CN').replaceAll('/','-')
      // 采用封装好的时间格式化方法
      appointment:wx.utils.formatDat(ev.detail)

    })
  },
  // 上传文件
  // 选择文件后触发
  async afteRead(event){
    // console.log(' 当前读取的文件',event.detail.file);
    const filePath =event.detail.file.tempFilePath
    const res = await wx.http.upload('/upload',{
      name:'file',
      filePath:filePath,
    })
    console.log('问题附件信息',res);
    this.setData({
      attachment:[...this.data.attachment,res.data]
    })
  },
  uploaderDeleteFile(e){
    console.log('删除图片的序号值',e.detail.index);
    this.setData({
      attachment:this.data.attachment.filter((v,index)=> index!== e.detail.index)
    })
  },
  beforeRead(ev){
    const{file,callback} = ev.detail
    // console.log('file',file);
    if (file.size >2*1024*1024) {
      wx.utils.toast('图片不能超过2M')
      return callback(false)
    } 
    callback(true)
  }
 
})
